<?php
namespace Home\Controller;

use Home\Model\ApplicationFormModel;

class BusinessTripController extends CommonController{
	protected $table = "application_form";
  	protected $pageSize = 12;

	public function index(){
    	$this->assign('btripType', $this->M->getBtripType());
		$this->assign('appType', $this->M->getType());
		$this->assign('appStatus', $this->M->getStatus());
		$this->assign('inspector', D('ApplicationForm')->getInspector());
		$this->assign('approval', D('ApplicationForm')->getApproval());
		$this->assign('app_names', D('ApplicationForm')->getAllCrmNames());
		$this->assign('app_name', D('ApplicationForm')->getAllNames());
		$this->assign('belongsDepartment',D('ApplicationForm')->getSelfDepartments());
		$this->assign('departName',D('ApplicationForm')->getSelfDepartment());
		$this->assign("userNames", D("ApplicationForm")->getUserNames());
		$this->display();
		
	}

	
  
  public function getList(){
	$this->setQeuryCondition();
    $count = (int)$this->M->where('type=5')->count();
    $this->setQeuryCondition();
    $list = $this->M->where('type=5')->page(I('get.p',0). ','. $this->pageSize)->order('id desc')->select();
    foreach($list as $k=>&$v){
			$re = $v['send_copy'];
			$res=explode(',',$re);
			$res1 = array();
			for($i=0;$i<count($res);$i++){
				$res1[] = D('user_info')->where(array('user_id'=>$res[$i]))->field('realname')->find();
			}
			
			
			$send_copy = "";
			$counts = count($res1);
			for($j = 0; $j < $counts; $j++){
			  $send_copy .= $res1[$j]['realname'].',';
			}
			$send_copy = substr($send_copy,0,strlen($send_copy)-1); 
			$v['send_copy'] = $send_copy;
		}
    $result = array('list'=>$list, 'count'=>$count);
		$this->ajaxReturn($result);

	}
  
  
  
  public function setQeuryCondition(){
  	if (isset($_GET['belongs_department'])) {
		$this->M->where(array("belongs_department"=> array('EQ', I('get.belongs_department'))));
	}else{
		$this->M->where(array("belongs_department"=> array('EQ', D('ApplicationForm')->getSelfDepartment())));
	}
  	
	if(I('get.app_status')){
		$this->M->where(array("app_status"=> array('EQ', I('get.app_status'))));
	}
	
	if (I('get.start1') && I('get.start2')) {
		$start1 = I('get.start1');
		$start2 = I('get.start2');
		$d = date('Y-m-d',strtotime($start2)+86400);
		$this->M->where(array("app_time"=> array('between', "$start1,$d")));
	}
	
	if (I('get.end1') && I('get.end2')) {
		$end1 = I('get.end1');
		$end2 = I('get.end2');
		$d = date('Y-m-d',strtotime($end2)+86400);
		$this->M->where(array("exp_time"=> array('between', "$end1,$d")));
	}
		
    $this->M->where( array('info_status'=>array('EQ', 1) ) );

  }
  
  /**
	 * 新增申请
	 */
	public function add() {
	  $_POST['app_creator'] = $_SESSION['account']['userInfo']['user_id'];
	  $_POST['belongs_department'] = D('ApplicationForm')->getSelfDepartment();
	  if (!empty($_POST) && $this->M->create($_POST, 1) && $this->M->add()) {
		$this->success(L('ADD_SUCCESS'));
	  } else {
		$this->error($this->M->getError());
	  }
	}

	/**
	 * 取消申请
	 */
	public function delete() {
		$id = I("post.ids");
		$id = $id[0];
		$res = D('application_form')->where(array('id'=>$id))->getField('check_time');
		if($res){
			$this->error('当前申请已在审核中,暂不可取消！');
		}else{
			if($this->M->data(array('app_process'=>-1))->where(array('id'=>array('in', $id)))->save()){
				$this->success('已取消!');
			}
			
		}
	}

}